package cn.yeziji.forum.dao.impl;

import cn.yeziji.forum.bo.file.FileBO;
import cn.yeziji.forum.common.AliCloudOss;
import cn.yeziji.forum.common.ForumPageResult;
import cn.yeziji.forum.common.key.CommonKey;
import cn.yeziji.forum.dao.base.ForumFileBaseDao;
import cn.yeziji.forum.dao.intf.ForumFileDao;
import cn.yeziji.forum.dao.iquery.FileQuery;
import cn.yeziji.forum.dto.file.ForumFilePagingDTO;
import cn.yeziji.forum.entity.ForumFileEntity;
import cn.yeziji.forum.exception.UploadException;
import cn.yeziji.forum.common.enums.status.UploadStatus;
import cn.yeziji.forum.utils.AliUtils;
import cn.yeziji.forum.utils.JwtOperaUtils;
import cn.yeziji.forum.utils.StrUtils;
import cn.yeziji.forum.utils.entity.AliOssConfig;
import org.springframework.stereotype.Repository;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 论坛文件管理逻辑层
 *
 * @author gzkemays
 * @since 2022/03/31 15:44
 */
@Repository
public class ForumFileDaoImpl extends ForumFileBaseDao implements ForumFileDao {
  @Resource AliCloudOss oss;
  @Resource HttpServletRequest request;

  @Override
  public String saveFile(MultipartFile file, String mode) {
    try {
      String ext = StrUtils.getLastSplitHasSplitAfterStr(file.getOriginalFilename(), CommonKey.DOT);
      String filename = DigestUtils.md5DigestAsHex(file.getInputStream());
      ForumFileEntity one =
          mapper.findOne(FileQuery.findFileByTargetQuery(buildUrl(mode, filename + ext)));
      if (one == null) {
        oss.setSavePath(mode);
        String url =
            AliUtils.uploadToOss(
                AliOssConfig.cloudOss(oss).fileName(filename).ext(ext).stream(file.getInputStream())
                    .build());
        ForumFileEntity entity = new ForumFileEntity();
        entity.setScope(mode);
        entity.setTarget(url);
        entity.setSize(file.getSize());
        entity.setSource(JwtOperaUtils.getUserNameByRequest(request));
        entity.setCreateTime(new Date());
        save(entity);
        return url;
      }
      return one.getTarget();
    } catch (IOException e) {
      e.printStackTrace();
    }
    throw new UploadException(UploadStatus.UPLOAD_FAIL);
  }

  @Override
  public void save(FileBO bo) {
    ForumFileEntity entity = bo.buildEntity();
    assert entity.getTarget() != null;
    ForumFileEntity one = mapper.findOne(FileQuery.findFileByTargetQuery(bo.getTarget()));
    if (one == null) {
      mapper().insert(entity);
    }
  }

  @Override
  public void del(FileBO bo) {
    ForumFileEntity one =
        mapper.findOne(FileQuery.findFileBySourceAndScopeQuery(bo.getSource(), bo.getSource()));
    if (one != null) {
      mapper.deleteById(one.getId());
    }
  }

  @Override
  public ForumFileEntity getEntityByTarget(String target) {
    return mapper.findOne(FileQuery.findFileByTargetQuery(target));
  }

  @Override
  public List<ForumFileEntity> getEntitiesBySource(String source) {
    return mapper.listEntity(FileQuery.findFileBySourceQuery(source));
  }

  @Override
  public ForumPageResult<ForumFileEntity> getFileEntitiesPage(ForumFilePagingDTO pagingDTO) {
    List<ForumFileEntity> entities = mapper.listEntity(FileQuery.findFilesPagingQuery(pagingDTO));
    pagingDTO.total(mapper.count(FileQuery.findFilesQuery(pagingDTO)));
    return new ForumPageResult<>(entities, pagingDTO);
  }

  @Override
  public List<ForumFileEntity> getEntitiesByScope(String scope) {
    return mapper.listEntity(FileQuery.findFileByScopeQuery(scope));
  }
}
